<template>
  <el-dialog
    v-model="visible"
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @keyup.enter="onSubmit()"
    >
      <el-form-item
        label="类目"
        prop="name"
      >
        <el-input v-model="dataForm.name" />
      </el-form-item>
      <el-form-item
        label="排序"
        prop="sortOrder"
      >
        <el-input v-model="dataForm.sortOrder" />
      </el-form-item>

      <el-form-item v-if="dataForm.id">
        <el-table
          :data="dataForm.infors"
          style="width: 100%"
        >
          <el-table-column
            label="公告标题"
            width="350"
          >
            <template #default="scope">
              <el-input v-model="scope.row.title" />
            </template>
          </el-table-column>
          <el-table-column
            prop="freeType"
            label="公告标题"
          >
            <template #default="scope">
              <el-input v-model="scope.row.content" />
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button
          type="primary"
          @click="onSubmit()"
        >确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { Debounce } from '@/utils/debounce'
const emit = defineEmits(['refreshDataList'])
const visible = ref(false)
const contentEnRef = ref(null)

// eslint-disable-next-line no-unused-vars
const validateTitle = (rule, value, callback) => {
  if (!value.trim()) {
    dataForm.value.title = ''
    callback(new Error('公告标题不能为空'))
  } else {
    callback()
  }
}
const dataRule = {
  title: [
    { required: true, message: '公告标题不能为空', trigger: 'blur' },
    { validator: validateTitle, trigger: 'blur' }
  ]
}

const dataForm = ref({
  title: null,
  content: null,
  status: 1
})
const dataFormRef = ref(null)
const init = (id) => {
  dataForm.value.id = id || 0

  visible.value = true
  nextTick(() => {
    dataFormRef.value?.resetFields()
    if (dataForm.value.id) {
      http({
        url: http.adornUrl('/admin/dao/noticeCate/info/' + dataForm.value.id),
        method: 'get',
        params: http.adornParams()
      }).then(({ data }) => {
        dataForm.value = data
        dataForm.value.infors = [{ title: '', content: '' }]
      })
    }
  })
}
defineExpose({ init })

/**
 * 表单提交
 */
const onSubmit = Debounce(() => {
  if (dataForm.value.id != 0) {
    dataForm.value.infors.forEach(element => {
      element.id = dataForm.value.id
      element.status = 1
      element.cateId = dataForm.value.id
    })
  } else {
    dataForm.value.infors = []
  }

  dataFormRef.value?.validate((valid) => {
    if (valid) {
      http({
        url: http.adornUrl('/admin/dao/noticeCate'),
        method: dataForm.value.id ? 'put' : 'post',
        data: http.adornData(dataForm.value)
      }).then(() => {
        ElMessage({
          message: '操作成功',
          type: 'success',
          duration: 1500,
          onClose: () => {
            visible.value = false
            emit('refreshDataList')
            dataForm.value.content = ''
          }
        })
      })
    }
  })
})
</script>
